package toOffer;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2024/1/28 11:09
 * @description <p>重排链表</p>
 */
public class toOffer4_4_26 {
    public static void main(String[] args) {
        int[] head = {1, 2, 3, 4};
        ListNode node = new ListNode();
        ListNode newHead = node;
        for (int i = 0; i < head.length; i++) {
            node.val = head[i];
            if (i != head.length - 1) {
                node.next = new ListNode(head[i + 1]);
                node = node.next;
            } else {
                node.next = null;
            }
        }

        reorderList(newHead);

        while (newHead != null) {
            System.out.print(newHead.val + " ");
            newHead = newHead.next;
        }
    }

    /**
     * 思路：
     * （1）原链表切两半，后面一半反转，然后交替插入前面的链表
     * （2）主要问题是怎么找到链表的中心点。可以定义一快一慢两个指针，快的走两个节点、慢的走一个
     * （3）需要考虑链表的节点数为单数和偶数的情况。链表节点数是偶数的话不用做额外的操作、单数可以考虑加个头节点
     *
     * @param head
     */
    public static void reorderList(ListNode head) {
        //初始化一个哨兵
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = dummy;
        int count = count(head);

        if (count % 2 == 0) {
            slow = slow.next;
            fast = fast.next;
            head = fun(slow, fast, head, count);
        } else {
            head = fun(slow, fast, head, count);
        }

    }

    /**
     * 做插入节点的操作
     *
     * @param slow
     * @param fast
     * @param head
     * @param count
     * @return
     */
    public static ListNode fun(ListNode slow, ListNode fast, ListNode head, int count) {

        ListNode tempHead = new ListNode();
        ListNode newHead = tempHead;
        while (fast != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        //反转后半部分链表
        slow = reverseNode(slow);
        //把反转的链表插入原来的前半部分链表
        for (int i = 0; i < count; i++) {
            //插入原节点
            if (i % 2 == 0) {
                tempHead.next = head;
                tempHead = tempHead.next;
                head = head.next;
            }else {
                //插入新节点
                tempHead.next = slow;
                tempHead = tempHead.next;
                slow = slow.next;
            }
        }
        return newHead;
    }

    /**
     * 反转链表
     * pre curr next
     * null 0    1
     * 0    1    2
     *
     * @param node
     * @return
     */
    public static ListNode reverseNode(ListNode node) {
        ListNode pre = null;
        ListNode curr = node;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        return pre;
    }

    /**
     * 计算链表节点数
     *
     * @param node
     * @return
     */
    public static int count(ListNode node) {
        int count = 0;
        while (node != null) {
            count++;
            node = node.next;
        }
        return count;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

    }
}
